package me.goldze.mvvmhabit.lks;

import android.app.Application;
import android.text.TextUtils;
import android.util.Log;

import java.util.Set;

import me.goldze.mvvmhabit.lks.dbmanager.IDBInitHelper;
import me.goldze.mvvmhabit.lks.lksBus.ILksUrlHelper;
import me.goldze.mvvmhabit.lks.module.IModuleHelper;
import me.goldze.mvvmhabit.lks.module.ModuleInit;
import me.goldze.mvvmhabit.lks.router.ClassUtils;
import me.goldze.mvvmhabit.lks.router.IRouterRoot;

public class LogisticsCenter {
    private static boolean registerByPlugin;
    private static final String ROUTE_ROOT_PAKCAGE = "me.goldze.mvvmhabit.lks.mylibrary.processor";

    private static void loadRouterMap() {
        registerByPlugin = false;
        //auto generate register code by gradle plugin:com.bruce.router
        // looks like below:
//         register(new ARouter..Root..modulejava());
    }

    private static void register(String className) {
        if (!TextUtils.isEmpty(className)) {
            try {
                Class<?> clazz = Class.forName(className);
                Object obj = clazz.getConstructor().newInstance();
                if (obj instanceof IDBInitHelper) {
                    ((IDBInitHelper) obj).register();
                } else if (obj instanceof ILksUrlHelper) {
                    ((ILksUrlHelper) obj).register();
                } else if (obj instanceof IModuleHelper) {
                    ((IModuleHelper) obj).register();
                } else if (obj instanceof IRouterRoot) {
                    ((IRouterRoot) obj).register();
                } else {
                    Log.e("LogisticsCenter", "register failed, class name: " + className);
                }
                registerByPlugin = true;
            } catch (Exception e) {
                Log.e("LogisticsCenter", "register class error:" + className);
            }
        }
    }

    public static void init(final Application context) {
        try {
            loadRouterMap();
            Log.e("bruce", "init: >>>>>>>>>>>>>>>" + registerByPlugin);
            if (!registerByPlugin) {
//                插件没有注册上走这里 比较耗时
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Set<String> classFileNames = ClassUtils.getFileNameByPackageName(context, ROUTE_ROOT_PAKCAGE);
                            for (String s : classFileNames) {
                                initMap(s);
                            }
                            ModuleInit.onCreate(context);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();
            } else {
                ModuleInit.onCreate(context);
            }
        } catch (Exception e) {

        }
    }

    private static void initMap(String className) {
        if (!TextUtils.isEmpty(className)) {
            try {
                Class<?> clazz = Class.forName(className);
                Object obj = clazz.getConstructor().newInstance();
                if (obj instanceof IDBInitHelper) {
                    ((IDBInitHelper) obj).register();
                } else if (obj instanceof ILksUrlHelper) {
                    ((ILksUrlHelper) obj).register();
                } else if (obj instanceof IModuleHelper) {
                    ((IModuleHelper) obj).register();

                } else if (obj instanceof IRouterRoot) {
                    ((IRouterRoot) obj).register();
                } else {
                    Log.e("LogisticsCenter", "register failed, class name: " + className
                            + " should implements one of IRouteRoot/IProviderGroup/IInterceptorGroup.");
                }
                registerByPlugin = true;
            } catch (Exception e) {
                Log.e("LogisticsCenter", "register class error:" + className);
            }
        }
    }


}
